package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.WorkDetail;
import com.android.equipment.dao.WorkDetail_;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.Query;
import io.objectbox.query.QueryBuilder;

public class DaoWorkDetail {
    static Box<WorkDetail> workDetailBox;
    private static DaoWorkDetail instance;

    private DaoWorkDetail() {
    }

    public static DaoWorkDetail getInstance() {
        if (instance == null) {
            instance = new DaoWorkDetail();
            workDetailBox = ObjectBox.get().boxFor(WorkDetail.class);
        }
        return instance;
    }

    public void addWorkDetail(WorkDetail workDetail) {
        workDetailBox.put(workDetail);
    }

    public void addWorkDetail(List<WorkDetail> workDetailList) {
        workDetailBox.put(workDetailList);
    }

    public void delWorkDetail(WorkDetail workDetail) {
        workDetailBox.remove(workDetail);
    }

    public List<WorkDetail> getWorkDetailByWorkId(long workId) {
        Query<WorkDetail> query = workDetailBox.query().equal(WorkDetail_.workId, workId).orderDesc(WorkDetail_.id).build();
        List<WorkDetail> relevanceList = query.find();
        query.close();
        return relevanceList;
    }

    public List<WorkDetail> getWorkDetailByWorkIdAndDeviceCode(long workId, String deviceCode){
        Query<WorkDetail> query = workDetailBox.query().equal(WorkDetail_.workId, workId).equal(WorkDetail_.deviceCode, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE).build();
        List<WorkDetail> workDetails = query.find();
        query.close();
        return workDetails;
    }

    public WorkDetail getWorkDetailByDetailId(long workDetailId) {
        return workDetailBox.get(workDetailId);
    }

    public void deleteAllWorkDetail() {
        workDetailBox.removeAll();
    }

    public void updateWorkDetail(WorkDetail workDetail) {
        workDetailBox.put(workDetail);
    }
}
